package socketTransmission;

import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.io.*;

/**
 * This program implements the server side to receive object from client.
 * 
 * @author Ganggao Zhu
 * @version 1.0 2011/10/19
 * 
 * 
 */

public class Server implements Runnable {

	private Message message;
	private boolean isStopped = false;
	SocketConnection socketConnection;

	Server(SocketConnection socketConnection) {
		this.socketConnection = socketConnection;
	}

	public void run() {
		
		while (!isStopped()) {
			
			message = socketConnection.receiveMessage();
			System.out.println("Server "+Thread.currentThread().getName()+" > "+message.message);
			
			message = new Message("Ok from "+Thread.currentThread().getName());
			socketConnection.sendMessage(message);
		}
	}
	
	//to know if the server stopped
	private synchronized boolean isStopped(){
		return this.isStopped;
	}
	
	//stop the server
	public synchronized void stop(){
		isStopped = true;
	}

}